package com.chrray.position.util.wx;


import com.chrray.position.util.redis.RedisUtils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 微信接口
 * @Data: Create in
 * @Modified By:
 */
@Component
@Slf4j
public class WxApiMessageUtil {


    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private Environment environment;
    public static WxApiMessageUtil wxApiUtil;


    @PostConstruct
    public void init() {
        wxApiUtil = this;
    }

    public static String getAccessToken() {

        Object wx_access_token = wxApiUtil.redisUtils.get("wx_access_token");
        String status = wxApiUtil.environment.getProperty("wx_access_token_status");
        if(!StringUtils.isEmpty(status)&&status.equals("1")){
            log.info("已关闭accesstoken 获取");
            return "";
        }
        String access_token = "";
        if (ObjectUtils.isEmpty(wx_access_token)) {
            access_token = WxApi.getAccessToken(wxApiUtil.environment.getProperty("appid"), wxApiUtil.environment.getProperty("secret"));
            log.info("wx_access_token,{}", access_token);
            wxApiUtil.redisUtils.set("wx_access_token", access_token, 6000);
        } else {
            access_token = wx_access_token.toString();
        }
        return access_token;
    }


    /**
     *  视频制作成功通知message
     * @param openid
     * @param time 2021年11月29日 17:53
     */
    public static void sendVideoSuccessMessage(String openid, String time,Long  pkRecordId) {
        String accessToken = getAccessToken();
        String tid = wxApiUtil.environment.getProperty("tx.wx.small.wxMessage1.temId");
        String page = wxApiUtil.environment.getProperty("tx.wx.small.wxMessage1.index");
        String miniProgramState = wxApiUtil.environment.getProperty("tx.wx.small.wxMessage1.miniProgramState"+pkRecordId);
        String remark = wxApiUtil.environment.getProperty("tx.wx.small.wxMessage1.msg");
        Map map = new HashMap();
        Map map1 = new HashMap();
        map1.put("value", remark);
        map.put("thing3", map1);
        Map map2 = new HashMap();
        map2.put("value", time);
        map.put("time1", map2);
        WxApi.sendMessage(accessToken, openid, tid, page, miniProgramState, map);
    }

    /**
     * 每被PK三次发送pkMessage
     * @param openid
     * @param userName  用户名
     * @param time 过去x天内
     */
    public static void sendPkMessage(String openid, String userName,String time,Long pkRecordId) {
        String accessToken = getAccessToken();
        String tid = wxApiUtil.environment.getProperty("tx.wx.small.wxMessage2.temId");
        String page = wxApiUtil.environment.getProperty("tx.wx.small.wxMessage2.index");
        String miniProgramState = wxApiUtil.environment.getProperty("tx.wx.small.wxMessage1.miniProgramState"+pkRecordId);

        Map map = new HashMap();

        Map map1 = new HashMap();
        map1.put("value", userName);
        map.put("thing1", map1);
        Map map2 = new HashMap();
        map2.put("value",time);
        map.put("short_thing3", map2);


        WxApi.sendMessage(accessToken, openid, tid, page, miniProgramState, map);
    }


    public static String getQrCode(String sceneStr, String page) {
        String accessToken = getAccessToken();
        String path = wxApiUtil.environment.getProperty("uploadFile.filePath");
        String visitUrl = wxApiUtil.environment.getProperty("uploadFile.visitUrl");
        String imgPath = wxApiUtil.environment.getProperty("uploadFile.imgPath");

        String url = WxApi.getQrCode(accessToken, sceneStr, page, path + imgPath);

//        String newPath = url.substring(url.indexOf(imgPath));

        return visitUrl + imgPath + url;
    }


}

